<?php
/**
 * This widget displays the progress sidebar.
 */
class ProgressWidget extends CWidget {

  /***
   * Id of the last progress item that has been added.
   */
  public $id_added_new = false;

  /**
   * Grab data and display on the view.
   */
  function run() {
    $progress_items = array();
    if (!Yii::app()->user->isGuest) {
      $id_user = (int)Yii::app()->user->id;
      $sql = "
      SELECT progress.*,project.name as project
      FROM progress
        INNER JOIN project
          ON project.id_project=progress.id_project
        INNER JOIN user
        ON user.id_user = project.id_user
      WHERE project.id_user=$id_user
      ".((int)$_REQUEST['id_project'] > 0 ? " AND progress.id_project={$_REQUEST['id_project']}":"")."
      ORDER BY progress.timestamp DESC";
      $cmd = Yii::app()->db->createCommand($sql);
      $result = $cmd->queryAll();
      if ($result && count($result) > 0) {
        $progress_items = $result;
      }
      $projects = Project::model()->findAll("id_user=$id_user");
    }
    
    if (!isset($projects) || $projects==null)
      $projects = array();
    $this->render('progressWidget',array('progress_items'=>$progress_items,'projects'=>$projects));
  }
}